﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Results;
using System.Windows.Forms;
using Google.Protobuf.WellKnownTypes;
using log4net;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using static CloudApiServer.Controllers.DemoController;

namespace CloudApiServer.Controllers
{
    /// <summary>
    /// API测试服务控制器
    /// </summary>
    [RoutePrefix("api")]
    public class DemoController : ApiController
    {
        /// <summary>
        /// Demo接口
        /// </summary>
        /// <returns></returns>
        [Route("Demo")]
        [HttpGet]
        public IHttpActionResult Demo()
        {
            try
            {
                return Json(new { code = 0, msg = "", data = $"已成功请求医保API服务！当前控制台版本号：{SystemInfo.Version} 编译时间：{SystemInfo.Versiontime}" });
            }
            catch (Exception ex)
            {

                return Json(new { code = -1, msg = $"服务端系统异常：服务端系统处理异常！请联系管理员！错误内容：{ex.Message} 异常来源：{ex.StackTrace}", data = "" });
            }
        }

        /// <summary>
        /// DemoPost接口
        /// </summary>
        /// <returns></returns>
        [Route("DemoPost")]
        [HttpPost]
        public IHttpActionResult DemoPost(HttpRequestMessage request)
        {
            try
            {
                var content = JsonConvert.DeserializeObject<Key>(request.Content.ReadAsStringAsync().Result);
                if (content.key == null)
                {
                    return Json(new { code = -1, msg = $"已成功请求API服务但Post入参格式有误，key值为空！传入接收：{request.Content.ReadAsStringAsync().Result}", data = "" });
                }
                else
                {
                    string key = content.key;
                    return Json(new { code = 0, msg = "", data = $"已成功请求API服务!Post请求成功!传入的key为:{key}" });
                }
            }
            catch (JsonReaderException)
            {
                return Json(new { code = -1, msg = $"已成功请求API服务但Post入参格式有误，系统未能正确反序列化！请检查入参格式！传入接收：{request.Content.ReadAsStringAsync().Result}", data = "" });

            }
            catch (Exception ex)
            {
                return Json(new { code = -1, msg = $"服务端系统异常：服务端系统处理异常！请联系管理员！错误内容：{ex.Message} 异常来源：{ex.StackTrace}", data = "" });
            }
        }

        public class Key
        {
            public string key { get; set; }
            // 其他属性  
        }
    }
}
